﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using Microsoft.Windows.Controls;
using Microsoft.Windows.Controls.Ribbon;
using ScrumMagic.Sprint;

namespace ScrumMagic
{
    /// <summary>
    /// Interaction logic for Page1.xaml
    /// </summary>
    public partial class SprintListForm : ScrumMagic.CommonForm
    {
        // test ribbon window
        private SprintListRibbon SprintListRibbon;

        private DataView context;

        public override RibbonTab RibbonContextualTab
        {
            get
            {
                SprintListRibbon.ribbonMenu.Tabs.Remove(SprintListRibbon.tabEdit);
                return SprintListRibbon.tabEdit;
            }
        }

        public SprintListForm()
        {
            InitializeComponent();

            SprintListRibbon = new SprintListRibbon();
            SprintListRibbon.FindButtonClicked += new EventHandler<TextEventArgs>(OnMenuFindButtonClicked);
            SprintListRibbon.AddButtonClicked += new EventHandler(OnMenuAddButtonClicked);
            SprintListRibbon.tabEdit.Label = "Sprint List";


            try
            {
                SqlCommand com = DBController.MainConnection.CreateCommand();
                com.CommandText =
                  "select Product.Id, Product.Name, [Version].Name AS Version, Product.Name AS Product, [Status].Name AS Status, Product.Comment " +
                    "from Product left join [Version] on Product.Version_Id = [Version].Id " +
                    "inner join Product on Product.Product_Id = Product.Id " +
                    "inner join Status on Product.Status_Id = Status.Id " +
                    "where Product.id in (select product_Id from RoleInProduct where User_Id = @userId);";
                com.CommandType = System.Data.CommandType.Text;
                com.Parameters.AddWithValue("@userId", CurrentUser.Instance.Id);
                context = DBController.ExecuteDataTable(com).DefaultView;
                this.DataContext = context;
            }
            catch (Exception ex)
            {
                MessageBox.Show("Can't get table Sprint",
                  "ScrumMagic",
                  MessageBoxButton.OK);
            }

        }

        private void OnMenuAddButtonClicked(object sender, EventArgs e)
        {
            AddTab addTab = new AddTab();
            addTab.Title = "Add Sprint";

            FormController.OpenForm(addTab, FormController.ContainerTypes.window);
        }

        void OnMenuFindButtonClicked(object sender, TextEventArgs e)
        {
            DataView dataSourse = context as DataView;
            dataSourse.RowFilter = string.Format("Name LIKE '{0}%'", e.Text);
        }

        private void btnAdd_Click(object sender, RoutedEventArgs e)
        {

        }

        private void btnView_Click(object sender, RoutedEventArgs e)
        {

        }

        private void btnEdit_Click(object sender, RoutedEventArgs e)
        {

        }

        private void dataGrid1_MouseDoubleClick(object sender, MouseButtonEventArgs e)
        {
           

            DataRowView row = sender as DataRowView;
            if (row==null) return;

            SprintCardForm card = new SprintCardForm((int)row["Id"]);
            card.Title = "Sprint Card";
            FormController.OpenForm(card, FormController.ContainerTypes.window);
            //card.Show();


        }

        private void CommonForm_Loaded(object sender, RoutedEventArgs e)
        {

        }

        private void dataGrid1_IsEnabledChanged(object sender, DependencyPropertyChangedEventArgs e)
        {

        }

        private void dataGrid1_CurrentCellChanged(object sender, EventArgs e)
        {
            //MessageBox.Show("CurrentCellchanged!!!");
        }

        private void dataGrid1_DataContextChanged(object sender, DependencyPropertyChangedEventArgs e)
        {
            //            MessageBox.Show("DataContextchanged!!!");
        }

        private void dataGrid1_SelectedCellsChanged(object sender, Microsoft.Windows.Controls.SelectedCellsChangedEventArgs e)
        {

            //MessageBox.Show("SelectedCellsChanged!!!");
        }

        private void dataGrid1_CellEditEnding(object sender, Microsoft.Windows.Controls.DataGridCellEditEndingEventArgs e)
        {
            //MessageBox.Show(rowSt);

            //MessageBox.Show(dataGrid1.SelectedItem.GetType().ToString());

            //MessageBox.Show("CellEditEnding!!!");
        }

        private void dataGrid_MouseDoubleClick(object sender, MouseButtonEventArgs e)
        {

            DataRowView row = (DataRowView)((DataGrid)sender).SelectedItem;
            
           // if (row == null) return;

            SprintCardForm card = new SprintCardForm((int)row["Id"]);
            card.Title = "Sprint Card";
            FormController.OpenForm(card, FormController.ContainerTypes.window);
            //card.Show();
        }

        //private void Item_GotFocus(object sender, RoutedEventArgs e)
        //{
        //    ListViewItem item = ((ListViewItem)(sender));
        //    this.ListView1.SelectedItem = item.DataContext;
        //}


    }
}

